#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>

int main()
{

    int id = fork();
    if(id == 0)
    {
        int cnt = 10;
        while(cnt--)
        {
            printf("我是子进程:pid:%d cnt:%d\n",getpid(),cnt);
            sleep(1);
        }
        exit(1);
    }

    int status = 0;
    while(1)
    {
        pid_t rid = waitpid(-1,&status,WNOHANG);
        if(rid >0)
        {
            printf("子进程status:%d\n",status);
            break;
        }
        else if(rid == 0)
        {
            printf("子进程未结束\n");
        }
        else
        {
            perror("waitpid");
        }
        sleep(1);
    }


    return 0;
}
